﻿using UnityEngine;

namespace Gp.Scripts.Core {
    [AutoLoad()]
    public class MusicManageSystem : BaseGameSystem {
        [SerializeField] private AudioClip[] defaultBattleMusic;
        [SerializeField] private AudioClip[] defaultSceneMusic;

        private const float DefaultVolume = .3f; 

        private void UpdateGlobalVolume() {
            // todo : 从存档中载入设置
        }

        
        
        public AudioSource CurMusic { get; private set; }
        
        
        protected override void OnInit() { }

        public override void OnStart() {
            // UpdateGlobalVolume();
            // PlaySceneMusic();
        }


        public void PlaySceneMusic() {
            if (defaultSceneMusic is not {Length: > 0}) return;
        }

        
        public void PlayBattleMusic(int index = -1) {
            if (defaultBattleMusic is not {Length: > 0}) return;

            AudioClip clip = null;
            if (index < 0 || index > defaultBattleMusic.Length) {
            }
            else {
                clip = defaultBattleMusic[index];
            }

            if (clip == null) return;
           
            PlayMusic(clip);
        }


        public void PlayMusic(AudioClip clip) {
            // if (CurMusic != null) MMSoundManager.Instance.FreeSound(CurMusic);
            // if (clip == null) {
            //     CurMusic = null;
            //     return;
            // }
            // var options = MMSoundManagerPlayOptions.Default;
            // options.Fade = true;
            // options.FadeDuration = 1f;
            // options.Loop = true;
            // options.MmSoundManagerTrack = MMSoundManager.MMSoundManagerTracks.Music;
            //
            // CurMusic = MMSoundManager.Instance.PlaySound(clip, options);
        }
        

        public void EndBattleMusic() {
            PlaySceneMusic();
        }
    }
}